package com.zut.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zut.dao.EmployeeDao;
import com.zut.pojo.Employee;
import com.zut.pojo.PageInfo;
import com.zut.service.EmployeeService;
import com.zut.utils.MybatisUtil;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 张晨
 * @date 2024/9/6 16:00
 * @desc
 */
public class EmployeeServiceImpl  implements EmployeeService {
    private EmployeeDao employeeDao;
    {
        employeeDao = MybatisUtil.getSqlSession().getMapper(EmployeeDao.class);
    }

    @Override
    public Integer deleteEmployeeById(Integer id) {
            return employeeDao.deleteById(id);
    }

    @Override
    public Integer addEmployee(Employee employee) {
            return employeeDao.insert(employee);
    }

    @Override
    public Integer updateById(Employee employee) {
            return employeeDao.updateById(employee);
    }


    @Override
    public PageInfo conditionSelect(Employee employee, int currentPage, int pageSize) {
        Page<Employee> list = null;
        List<Employee> employees = new ArrayList<>();
        try {
            PageHelper.startPage(currentPage, pageSize);
            list = employeeDao.selectByAccountAndNicknameAndTelephone(employee);
            for (Employee employee1 : list) {
                employees.add(employee1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        PageInfo<Employee> pageInfo = new PageInfo<>();
        pageInfo.setList(employees);
        pageInfo.setTotal(list.getTotal());
        return pageInfo;
    }
}
